<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['PAD']."Modules/FTK9D4D0/modconf.mex");
MYTCONN();

	# Init Vars
	$dataSet = '';
	$colmodel = '';
	
	$flt_Corresp = '';
	$flt_Method = '';
	$flt_Channel = '';
	
	$lang['trnChannel'][1] = 'SAIDA';
	$lang['trnChannel'][2] = 'ENTRADA';
	$lang['trnMethod'][1] = 'FILETRANSFER';
	$lang['trnMethod'][2] = 'WEBSERVICE';
	$lang['trnFlag'][2]='Activo';
	$lang['trnFlag'][1]='Inactivo';
	$lang['trnFlag'][5]='Anulado';
	$lang['trnFlag'][1]='Aberto';
	$lang['trnFlag'][2]='Fechado';
	$lang['trnFlag'][5]='Anulado';
	
	
	# Filters
        if(!isset($_POST['TransactDateStart'])){$_POST['TransactDateStart']=date("Y-m-d");$_POST['TransactDateEnd']=date("Y-m-d");}
	if(isset($_POST['TransactMethod']) && $_POST['TransactMethod'] > 0){$flt_Method=' AND tcmtMethod='.$_POST['TransactMethod'];}
	if(isset($_POST['TransactChannel']) && $_POST['TransactChannel'] > 0){$flt_Channel=' AND tcmtChannel='.$_POST['TransactChannel'];}
	if(isset($_POST['TransactCorrespondent']) && $_POST['TransactCorrespondent'] > 0){$flt_Corresp=' AND tcmtCorrespondent='.$_POST['TransactCorrespondent'];}
        $flt_Time=" AND DATE(stk_PrcDateTime) BETWEEN '".$_POST['TransactDateStart']."' AND '".$_POST['TransactDateEnd']."' ";

	# Select View
	if(!isset($_GET['View'])){$_GET['View']=1;}
	$GridView = $_GET['View'];

	# Load DataSet
	if($GridView==1){
	$colmodel = '<tr>
			<td></td>
			<td style="text-align:center;width:30px"></td>
			<td>PID</td>
			<td>PNAME</td>
			<td>Correspondente</td>
			<td>Data</td>
			<td>Canal</td>
			<td>Metodo</td>
			<td>Estado</td>
		</tr>';

	$GET = $GLOBALS['MYT']->db_array("SELECT stk_ID, stk_ProcessID, stk_ProcesstoUser, stk_Flag, stk_PrcDateTime, stk_RegID,
							PID, PNAME, FLAG, PDESCRIPTION,
							tcmtID, tcmtPID, tcmtMethod, tcmtChannel, tcmtCorrPNAME, tcmtFlag, 
							CorrNombre, tcmtCorrespondent
						FROM systemoffice.sys_tasks 
					INNER JOIN systemoffice.sys_process ON sys_process.PID = stk_ProcessID
					INNER JOIN officeschema.trn_corrmethods ON tcmtPID = PID AND tcmtCorrespondent = stk_RegCorr
					INNER JOIN officeschema.corresponsales ON CorrNumero = tcmtCorrespondent
					WHERE PNAME LIKE 'TRANSACT%' {$flt_Time}{$flt_Corresp}{$flt_Method}{$flt_Channel}");
	$genPr = 'pr=ftpsync&';
	}elseif($GridView==2){
	$genPr = '';
	$colmodel = '<tr>
			<td style="text-align:center;width:30px"></td>
			<td style="text-align:center;width:30px"></td>
			<td>PID</td>
			<td>PNAME</td>
			<td>Correspondente</td>
			<td>Activo a</td>
			<td>Canal</td>
			<td>Metodo</td>
			<td>Estado</td>
		</tr>';
	
	$GET = $GLOBALS['MYT']->db_array("SELECT 
							tcmtID, tcmtPID, tcmtMethod, tcmtChannel, tcmtCorrPNAME, tcmtFlag AS stk_Flag, tcmtLastRun AS stk_PrcDateTime, 0 AS stk_RegID,
							PNAME,
							CorrNombre,tcmtCorrespondent
						FROM officeschema.trn_corrmethods
					INNER JOIN systemoffice.sys_process ON tcmtPID = PID
					INNER JOIN officeschema.corresponsales ON CorrNumero = tcmtCorrespondent
					WHERE tcmtFlag = 2 {$flt_Corresp}{$flt_Method}{$flt_Channel}");
	}
	
	for($i=1;$i<=$GET[0];$i++){
	if(isset($GET[$i]['stk_ID'])){$tskID='tskID='.$GET[$i]['stk_ID'].'&';}else{$tskID='';}
	$dataSet .= "<tr>
			<td><div id=\"ExtAct".$i."\">";
	if($GET[$i]['stk_Flag']==1 && $tskID>0 || $tskID==0 && $GET[$i]['stk_Flag']==2){
	    $dataSet .= rt_field('button','ED','Exec','Exec','','','','"
				onclick="document.TrnTaskForm.action=\'_response/transact.php?PID='.$GET[$i]['tcmtPID'].'&'.$genPr.$tskID.'CorrID='.$GET[$i]['tcmtCorrespondent'].'&TRNV='.$GET[$i]['stk_RegID'].'\';
				subform(\'TrnTaskForm\',\'ExtAct'.$i.'\');');
	}			
	$dataSet .= "</div></td>
			<td><img src=\"Shared/Graphics/arw{$GET[$i]['tcmtChannel']}{$GET[$i]['stk_Flag']}.png\" /></td>
			<td>{$GET[$i]['tcmtPID']}</td>
			<td>{$GET[$i]['PNAME']}</td>
			<td>{$GET[$i]['CorrNombre']}</td>
			<td>{$GET[$i]['stk_PrcDateTime']}</td>
			<td>{$lang['trnChannel'][$GET[$i]['tcmtChannel']]}</td>
			<td>{$lang['trnMethod'][$GET[$i]['tcmtMethod']]}</td>
			<td>{$lang['trnFlag'][$GET[$i]['stk_Flag']]}</td>
		</tr>";
	
	}
	
	# Print HTML
	echo "<table class=\"gridtable\">$colmodel$dataSet</table>";